package com.qiyu.tech.pay.queue;

import com.alibaba.fastjson.JSON;
import com.qiyu.core.cache.redis.client.RedisService;
import com.qiyu.tech.pay.queue.model.DelayTaskInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 8:46 上午 2021/4/2
 */
@Component("redisReadyQueue")
public class RedisReadyQueue implements RedisQueue<DelayTaskInfo> {

    @Resource
    private RedisService redisService;

    private static final String REDIS_READY_QUEUE_KEY = "redis:ready:queue:pay";

    @Override
    public DelayTaskInfo pop() {
        String item = redisService.rpop(REDIS_READY_QUEUE_KEY);
        if(item != null) {
            return JSON.parseObject(item,DelayTaskInfo.class);
        }
        return null;
    }

    @Override
    public boolean put(DelayTaskInfo delayTaskInfo) {
        Long id = -1L;
        id = redisService.lpush(REDIS_READY_QUEUE_KEY, JSON.toJSONString(delayTaskInfo));
        return id > 0;
    }

    @Override
    public boolean delete(DelayTaskInfo delayTaskInfo) {
        return true;
    }
}
